package com.lianzhihui.romeex.ui.me.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.PreferenceUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me.dialog
 * @ClassName: UpDateVersionDialog
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/3 10:40
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/3 10:40
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ChoseCurrencyDialog extends BaseDialog implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.radio_layout)
    RadioGroup radioLayout;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    String checkedString;
    @BindView(R.id.tv_title)
    TextView title;
    @BindView(R.id.rb_cny)
    RadioButton rbCny;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.rb_us)
    RadioButton rbUs;
    @BindView(R.id.line2)
    View line2;
    Callback callback;
    private String stringPreference;

    public ChoseCurrencyDialog(@NonNull Context context) {
        super(context);
    }

    public ChoseCurrencyDialog(Context context, int style) {
        super(context, style);
    }

    public void setDialogListener(Callback callback) {
        this.callback = callback;
    }

    @Override
    public void setCancelable(boolean flag) {
        super.setCancelable(flag);
    }


    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_change_currency;
    }

    @Override
    protected void initView() {
        radioLayout.setOnCheckedChangeListener(this);
        checkedString = "";
        stringPreference = PreferenceUtils.getString(AppConstants.CURRENCY_TYPE_KEY, "");
        if (!stringPreference.isEmpty()) {
            if (stringPreference.equals(AppConstants.CNY)) {
                rbCny.setChecked(true);
                rbUs.setChecked(false);
            } else if (stringPreference.equals(AppConstants.USD)) {
                rbUs.setChecked(true);
                rbCny.setChecked(false);
            }
        }
    }


    @OnClick({R.id.iv_close, R.id.tv_confirm})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.iv_close:
                    dismiss();

                    break;
                case R.id.tv_confirm:
                    if (null != callback) {
                        if (checkedString.isEmpty()) {
                            //未操作
                            dismiss();
                        } else {
                            //已经选择
                            if (checkedString.equals(stringPreference)) {
                                //选择结果和存储结果相同则不处理
                                dismiss();
                            } else {
                                callback.choseResult(checkedString);
                            }
                        }

                    }
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getCheckedRadioButtonId()) {
            case R.id.rb_cny:
                checkedString = AppConstants.CNY;
                break;
            case R.id.rb_us:
                checkedString = AppConstants.USD;
                break;
            default:
                break;
        }
    }

    public interface Callback {
        void choseResult(String s);
    }
}
